package com.cb.wlm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cb.service.ComboMidService;
import com.cb.service.UserComboService;
import com.cb.service.UserEcardsService;
import com.cb.service.UserService;

import com.jia.dto.LoginDto;
import com.jia.entity.User;
import com.jia.entity.UserCombo;
import com.jia.entity.UserEcards;
import com.jia.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 王利明
 */
@RestController
@RequestMapping("/api")
public class LoginController {
    @Autowired
    private RedisTemplate<String,String>redisTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private UserEcardsService userEcardsService;
    @Autowired
    private UserComboService userComboService;
    @Autowired
    private ComboMidService comboMidService;
    /**
     * 登录
     * 根据手机号、验证码登录
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        System.err.println(loginDto);
            LoginVo login = userService.login(loginDto);
        return Result.build(login, ResultCodeEnum.SUCCESS);
    }

    /**
     * getuserInfo
     * @param token
     * @return
     */
    @GetMapping("/userinfo")
    public Result<UserInfo> getUserInfo(@RequestHeader("token")String token){
        System.err.println(token);
        UserInfo userInfo=userService.getUserInfo(token);
        return Result.build(userInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取手机验证码
     * @param phone
     * @return
     */
    @GetMapping("/getCode")
    public Result getCode(String phone) {
        System.err.println(phone);
        String codes="";
        Random random = new Random();
        int randomNumber = random.nextInt(900000) + 100000;
        codes=String.valueOf(randomNumber);
        Map<String,Object> map=new HashMap<>(10);
        map.put("codes",codes);
//        boolean b=userService.send(phone,map);
        boolean b=true;
        if(b){
            //存入redis，有效期1分钟
            this.redisTemplate.opsForValue().set(phone+"_"+codes, randomNumber+"",1, TimeUnit.MINUTES);
            return Result.build("发送成功", ResultCodeEnum.SUCCESS);
        }else{
            return Result.build("发送失败", ResultCodeEnum.DATA_ERROR);
        }
     }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        System.err.println(user);
        boolean b=userService.register(user);
        if(b){
            return Result.build("注册成功", ResultCodeEnum.SUCCESS);
        }else{
            return Result.build("注册失败", ResultCodeEnum.DATA_ERROR);
        }
    }

    /**
     * 实名认证
     * @param user
     * @return
     */
    @PutMapping("/realName")
    public Result unregister(@RequestBody User user){
        System.out.println(user);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",user.getPhone());
         User user1 =  userService.getOne(queryWrapper);
        if(user1 ==null){
            userService.save(user);
            return Result.build("实名成功", ResultCodeEnum.SUCCESS);
        }else{
            userService.updateByPhone(user);
            return Result.build("实名成功", ResultCodeEnum.SUCCESS);
        }

    }

    /**
     * 手机号唯一
     * @param phone
     * @return
     */
    @GetMapping("/phoneUnique")
    public Result phoneUnique(String phone){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user =  userService.getOne(queryWrapper);
        if(user ==null){
            return Result.build("手机号可用", ResultCodeEnum.SUCCESS);
        }else{
            return Result.build("手机号不可用", ResultCodeEnum.DATA_ERROR);
        }
    }

    /**
     * 验证身份证唯一
     * @param cardid
     * @return
     */
    @GetMapping("/cardid")
    public Result cardid(String cardid){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cardid",cardid);
        User user =  userService.getOne(queryWrapper);
        if(user ==null){
            return Result.build("身份证号可用", ResultCodeEnum.SUCCESS);
        }else{
            return Result.build("身份证号被占用", ResultCodeEnum.DATA_ERROR);
        }

    }

    /**
     * 获取名下电卡
     * @param id
     * @return
     */
      @GetMapping("/dian")
    public Result dian(String id){
        QueryWrapper<UserEcards> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",id);
          List<UserEcards> list = userEcardsService.list(queryWrapper);
          return Result.build(list, ResultCodeEnum.SUCCESS);
      }

    /**
     * 获取名下的套餐
     * @param id
     * @return
     */
      @GetMapping("/taocan")
    public Result taocan(String id){
          QueryWrapper<UserCombo> queryWrapper=new QueryWrapper<>();
          queryWrapper.eq("uid",id);
          List<UserCombo> list = userComboService.list(queryWrapper);
          return Result.build(list, ResultCodeEnum.SUCCESS);
      }

    /**
     * 套餐类型
     * @param id
     * @return
     */
      @GetMapping("/taocanType")
    public Result taocanType(String id){
         ComBoVo comboVo=comboMidService.find(id);
          return Result.build(comboVo, ResultCodeEnum.SUCCESS);
      }
}
